const App = getApp()
const db = wx.cloud.database()
const _ = db.command
let that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    systemInfo: {},
    scrollId: '',
    // 消息列表
    chatList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    that = this
    let data = JSON.parse(decodeURIComponent(options.passData));
    console.log('传值后', data);
    let UserInfo = App.cache.getUserInfo()
    console.log('获取用户信息:', UserInfo);
    delete UserInfo._id
    that.setData({
      chatType: data.chattype,
      chatName: data.name,
      UserInfo: UserInfo
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          systemInfo: res
        })
      }
    })
    this.getMsgDetails()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 根据消息类型获取消息列表详情
  getMsgDetails() {
    db.collection('message').where({
        msgType: that.data.chatType,
        'MsgMembers.account': that.data.UserInfo.account
      })
      .get({
        success: function (res) {
          console.log(res.data)
          let list = res.data
          list.sort((x, y) => x.timeTs - y.timeTs)
          that.setData({
            chatList: list
          })

          let len = parseInt(that.data.chatList.length)
          that.setData({
            scrollId: 'y-' + parseInt(len - 1)
          })
        }
      })
  }
})